<?php

namespace App\Models\Logs\UserLoginLog\Repositories;

use App\Models\Logs\UserLoginLog\Repositories\Interfaces\UserLoginLogRepositoryInterface;
use App\Models\Logs\UserLoginLog\UserLoginLog;
use App\Trip\Repository\BaseRepository;
use Illuminate\Support\Facades\Cache;

class UserLoginLogRepository extends BaseRepository implements UserLoginLogRepositoryInterface
{

    public function __construct(UserLoginLog $model)
    {
        parent::__construct($model);
    }

    public function record(array $data): ?UserLoginLog
    {
        $cache_key = "UserLoginLog:{$data['user_id']}";
        Cache::put($cache_key, 1, config('trip.ttl.day'));
        return $this->isRecorded($data['user_id']) ? null : $this->create($data);
    }

    /**
     * @param string $user_id
     * @param array  $data 可以用来去除相同的记录
     * @return bool
     */
    public function isRecorded(string $user_id, array $data = []): bool
    {
        $cache_key = "UserLoginLog:{$user_id}";
        return Cache::has($cache_key);
    }

}